/*
made in liguang.tang

此类是管理主视图view类

*/
package view
{
	import com.greensock.*;
	import com.greensock.easing.*;
	
	import control.CardMachine;
	import control.GameRule;
	
	import event.CustomEvent;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.net.URLRequest;
	import flash.text.TextField;
	
	import view.UIlayer.ButtonViewLayer;
	import view.UIlayer.CardView;
	import view.UIlayer.CardViewLayer;
	import view.UIlayer.LogoView;
	import view.UIlayer.ScoreboardView;
	
	import vo.CardVo;
	import vo.GameCardsVO;
	import vo.GameMode;
	
	
	public class MainView extends Sprite
	{		
		private var buttonView:ButtonViewLayer = new ButtonViewLayer();	
		private var scoreboardView:ScoreboardView = new ScoreboardView();	
		private var cardviewLayer:CardViewLayer = new CardViewLayer();		
		private var cardMachine:CardMachine = new CardMachine();			
		private var gameRule:GameRule = new GameRule();		
		
		private var logoView:LogoView = new LogoView();
		private var gm:GameMode = GameMode.getInstance();
		
		public function MainView()
		{
			init();
		}
		
		private function init():void{
			var loaderBackGround:Loader = new Loader();
			logoView.addEventListener(LogoView.HIGAGA,putMyLogo);
			loaderBackGround.load(new URLRequest("assets/images/backgound.jpg"));
			addChild(loaderBackGround);			

			addChild(cardviewLayer);
			addChild(buttonView);
			addChild(scoreboardView);					

			gameRule.addEventListener( CustomEvent.GAME_START,gameStartHandler);	
			gameRule.addEventListener( CustomEvent.GAME_END,gameEndHandler);
			gameRule.addEventListener( CustomEvent.GAME_DISPLAY,gameDisplayHandler);
			gameRule.addEventListener( CustomEvent.GAME_DISPLAY2,gameDisplay2Handler);
			gameRule.addEventListener( CustomEvent.GAME_DISPLAY_SCORE,gameDisplayScoreHandler);
						
			buttonView.addEventListener(CustomEvent.ONCLIK_START,btnStartHandler);
			buttonView.addEventListener(CustomEvent.ONCLIK_SHOW,btnEndHandler);			
			buttonView.addEventListener(CustomEvent.ONCLIK_ADD,btnAddHandler);
			buttonView.addEventListener(CustomEvent.ONCLIK_SUB,btnSubHandler);
		}
		
		private function gameEndHandler(e:Event):void{
			buttonView.updateGameEnd();
			scoreboardView.updateGameEnd();
			cardviewLayer.displayCard();				
		}		
		private function gameStartHandler(e:CustomEvent):void{
			buttonView.updateGameStart();	
			cardviewLayer.displayCard();
			scoreboardView.updateGameStart();
		}
		private function gameDisplayHandler(e:CustomEvent):void{			
			buttonView.updateGameStart2();
			cardviewLayer.displayCard();				
		}	
		private function gameDisplay2Handler(e:CustomEvent):void{			
			cardviewLayer.displayCard();
		}	
		private function gameDisplayScoreHandler(e:CustomEvent):void{			
			scoreboardView.updateScore();
		}	
		
		///////////////
		
		private function btnStartHandler(e:CustomEvent):void{
			if(e.BTNState == 1){		
				gameRule.gameStart();
			}
			else if( e.BTNState == 2){
				gameRule.gameGetCard();
			}
			else{				
				gameRule.gameRreStart();
			}			
		}
		
		public function btnEndHandler(e:CustomEvent):void{		
			gameRule.gameEnd();
		}
		
		private function btnAddHandler(e:Event):void{
			gameRule.add();
		}
		private function btnSubHandler(e:Event):void{
			gameRule.sub();
		}
		private function putMyLogo( event:Event ):void{
			addChild(logoView);
			logoView.x = 100;
			logoView.y = 563;
			logoView.scaleX = 1.8;
			logoView.scaleY = 1.8;
		
		}
	}
}